# -*- coding: UTF-8 -*-
# Author: Slp
# @Date : 2021-08-25

import unittest

import yaml

from src.UItest.login_page import LoginPage
from src.common.ddtNew import ddt, file_data
from src.UItest.base_page import BasePage
from src.common.screenshot import takeScreenshot, createDir


@ddt
class LoginTest(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.driver = BasePage.get_chrome(cls)
        cls.lp = LoginPage(cls.driver)

    @file_data('../../data/login.yaml')
    def test_01(self, **kwargs):
        """
        登录成功校验
        :param kwargs:
        :return:
        """
        self._testProjectName = '车位通'  # 测试报告中展示
        self._testMethodDoc = '登陆验证'  # 测试报告中展示
        self._testMethodName = 'test_login'  # 测试报告中展示
        self._testMethodUrl = ''  # 测试报告中展示
        username = kwargs['username']

        password = kwargs['password']
        url = kwargs['url']
        self.lp.login(url, username, password)
        cookie = self.driver.get_cookies()
        print("登录成功，对应的cookie信息为：" + str(cookie))
        cookie_dir = {"cookie": cookie}
        with open('../../data/cookie.yaml', 'w', encoding='utf-8') as f:
            yaml.dump(cookie_dir, f, allow_unicode=True)  # 保存登录cookie值，为后续其他操作使用，不用重复登录
            f.close()
        ac = BasePage.get_title(self)  # 获取页面名称作为断言信息
        self.assertEqual(ac, "车位通")
        takeScreenshot(self.driver, createDir("jyy"), "login")  # 调用截图方法截图执行后页面并保存执行结果页面

    @classmethod
    def tearDownClass(cls):
        BasePage.quit(cls)


if __name__ == "__main__":
    unittest.main(verbosity=2)
